package com.ssly.weixin.xichang.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Helper {
    public static String uploadFile(MultipartFile multipartFile, String saveDir) throws IOException {
        if (null != multipartFile && !multipartFile.isEmpty()) {
            String originalFilename = multipartFile.getOriginalFilename();
            String[] allowExt = {".mp4", ".avi", ".jpg", ".png"};
            //3新名称(uuid随机数加上后缀名)
            assert originalFilename != null;
            String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (Arrays.asList(allowExt).contains(ext)) {
                String date=saveDir.substring(saveDir.lastIndexOf(File.separator)+1);
                String newfileName = UUID.randomUUID() + ext;
                File file = new File(saveDir, newfileName);
                multipartFile.transferTo(file);
                return '/'+date+'/'+newfileName;
            }else {
                return null;
            }
        }
        return null;
    }

    public static boolean checkPhone(String phone) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
        m = p.matcher(phone);
        b = m.matches();
        return b;
    }
}
